<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class store_api {
	
	public function __construct() {
		$this->pz = &get_instance();
	}
	

	public function storeres($store_id,$type='store'){
		$store_id = abs(intval($store_id));
		$store_id = sprintf("%09d", $store_id);
		$dir1 = substr($store_id, 0, 3);
		$dir2 = substr($store_id, 3, 2);
		$dir3 = substr($store_id, 5, 2);
		$info = array();
		$dir = 'data/'.$type.'/'.$dir1.'/'.$dir2.'/'.$dir3.'/';
		$filename = substr($store_id, -2);
		return array('dir'=>$dir,'css'=>$filename.'_style.css','bg'=>$filename.'_bg.jpg','avatar'=>$filename.'_avatar.jpg');
	}

	public function getStoreById($id){
		$storedb = load_db("store");
		$store = $storedb->getStoreByid($id);
		$store = $this->prepareStore($store);
		return $store;
	}

	public function prepareStore($store){
		if($store){
			$shareapi = load_api('share');
			$pager = &load_class('Pager', 'core');
			$res = $this->storeres($store['store_id']);
			$dirpath = BASEPATH.$res['dir'];
			$store['css'] = file_exists($dirpath.$res['css'])?base_url().$res['dir'].$res['css']:false;
			$store['avatar'] = file_exists($dirpath.$res['avatar'])?base_url().$res['dir'].$res['avatar']:base_url().'assets/img/avatar_large.jpg';
			$store['link'] = site_url('store/show?storeid='.$store['store_id']);
			if($store['settings']){
				$store['settings'] = unserialize($store['settings']);
			}
			$share_conditions['store_id'] = $store['store_id'];
			$share_conditions['channel'] = 'review';
			$shares = $shareapi->search($share_conditions,1,9,null);
			$store['shares'] = $shareapi->prepareShares($shares);
			$pageData = $pager->getPager();
			$store['numbershares'] = $pageData['total_count'];
			$store['keywords'] = ($store['keyword'])?encodekeywordsarray(explode(',', $store['keyword'])):null;
			$store['totalcomments'] = load_db('comment/storecomment')->countbystore($store['store_id']);
			$store['isowner'] = ($this->pz->cuser['uid']==$store['user_id'])?true:false;
			return $store;
		}
		return false;
	}

	public function delete($id){
		$storedb = load_db("store");
		$condition['store_id'] = $id;
		$result = load_db("store/favstore")->delete($condition);
		$result = load_db("store")->delete($condition);
		$res = $this->storeres($id);
		$dirpath = BASEPATH.$res['dir'];
		file_exists($dirpath.$res['css']) && unlink($dirpath.$res['css']);
		file_exists($dirpath.$res['avatar']) && unlink($dirpath.$res['avatar']);
		return $result;
	}

	public function searchFull($conditions,$page,$pagesize,$sort=null){
		$stores = load_db("store")->search($conditions,$page,$pagesize,null,$sort);
		if($stores){
			$stores = $this->preparestores($stores);
		}
		return $stores;
	}

	public function preparestores($stores){
		foreach ($stores as $key=>$store){
			$stores[$key] = $this->prepareStore($store);
		}
		return $stores;
	}


	public function getsuggestactivities($num=6){
		$shares = pcache('r', 'suggestactivities');
		if(!$shares){
			$shareapi = load_api('share');
			$shares = $shareapi->search(array('channel'=>'activity'),1,$num,' ptx_share.create_time ');
			pcache('w', 'suggestactivities',$shares,3*3600*24);
		}
		return $shares;
	}
}

